package edu.zut.hb.software.together.controller;

import java.util.HashMap;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import edu.zut.hb.software.base.utils.R;
import edu.zut.hb.software.base.web.spring.controller.GenericController;
import edu.zut.hb.software.together.domain.Friend;
import edu.zut.hb.software.together.service.FriendService;


@RestController
@RequestMapping(value = "/friend", produces = { MediaType.APPLICATION_JSON_VALUE })
public class FriendController extends GenericController<Friend, Long, FriendService> {

    public static final Logger logger = LogManager.getLogger(FriendController.class.getName());
    FriendService friendService;

    @Autowired
    public void setFriendService(FriendService friendService) {
        this.friendService = friendService;
        this.manager=friendService;
    }
    @ResponseBody
    @RequestMapping(value="/findAll/{page}/{pageSize}",method = {RequestMethod.GET,RequestMethod.POST})
    public R findAllFriend(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize){
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of(page,pageSize, sort);

        Page<Friend> list = friendService.findAll(pageable);

        HashMap<String,Object> map = new HashMap<>();
        map.put("number",list.getTotalElements());
        map.put("userList",list.getContent());
        logger.info("----------->"+list.getContent());
        //map集合转化为JSON字符串进行数据传输
//        String res = JSON.toJSONString(map);
        return R.ok().data(map);
    }

}
